﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Macds.Core.Validation
{
    public abstract class ValidatorDecorator<T>
    {
        private T obj;
        protected abstract IEnumerable<IValidationUnit> GetValidations();

        public T Instance
        {
            get
            {
                return this.obj;
            }
            set
            {
                this.obj = value;
            }
        }

        protected void ValidateItems()
        {
            foreach (var validation in GetValidations())
            {
                validation.Instance = this.obj;

                if (validation.Validate() == false)
                {
                    throw validation.InternalException;
                }

                this.obj = (T)validation.Instance;
            }
        }

        public T Validate()
        {
            ValidateItems();
            return this.Instance;
        }
    }
}
